const { app, BrowserWindow } = require("electron");

// 项目搭建
/* //当 APP 启动之后执行窗口创建等操作   app.whenReady()返回的是一个promise
app.whenReady().then(() => {
  const mainWin = new BrowserWindow({
    width: 600,
    height: 400,
  });

  //在当前窗口中加载指定的界面让它显示具体的内容
  mainWin.loadFile("index.html");

  //监听当前窗口关闭
  mainWin.on("close", () => {
    console.log("close");
  });
});

// 监听所有窗口关闭
app.on("window-all-closed", () => {
  console.log("window-all-closed");
  //退出应用
  app.quit();
}); */

//生命周期
//创建窗口
//执行命令后
//1.ready
//2.dom-ready
//3.did-finish-load
//点击关闭窗口
//4.this windw is closed
//5.window-all-closed
//如果没有监听window-all-closed就会监听以下生命周期，或者在window-all-closed监听里面加一个 app.quit()
//6.before-quit
//7.will-quit
//8.quit
/* function createWindow() {
  let mainWin = new BrowserWindow({
    width: 800,
    height: 400,
  });
  //在当前窗口中加载指定的界面让它显示具体的内容
  mainWin.loadFile("index.html");

  //可以控制dom原素的对象
  mainWin.webContents.on("did-finish-load", () => {
    console.log("did-finish-load ");
  });

  mainWin.webContents.on("dom-ready", () => {
    console.log("dom-ready ");
  });

  //监听当前窗口关闭
  mainWin.on("close", () => {
    console.log("this windw is closed ");
    //释放窗口，释放内存
    mainWin = null;
  });
}

//监听窗口 调用上面的函数
app.on("ready", () => {
  console.log("ready");
  createWindow();
});

//监听所有窗口关闭
app.on("window-all-closed", () => {
  console.log("window-all-closed");
  app.quit();
});

//监听退出相关的
app.on("before-quit", () => {
  console.log("before-quit");
});

app.on("will-quit", () => {
  console.log("will-quit");
});

app.on("quit", () => {
  console.log("quit");
});
 */

// 窗口尺寸
// 创建窗口独立成一个函数
function createWindow() {
  let mainWin = new BrowserWindow({
    width: 800,
    height: 400,
  });
  mainWin.loadFile("index.html");
  mainWin.on("close", () => {
    console.log("mainWin close");
    mainWin = null;
  });
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
  console.log("window-all-closed");
  app.quit();
});
